import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Res,
  Headers,
} from '@nestjs/common';
import { Response } from 'express';
import { MultipartFile } from '@fastify/multipart';
import { MediaService } from '../services';
import { Public } from '@/modules/core/decorators/publicGuards';
import { FastifyReply } from 'fastify';
interface FormDataFile {
  file: MultipartFile;
  dir: any;
}

//上传文件 1 先生成文件路径
// 将文件存入到miedia数据库
// 返回文件的id
@Controller('media')
export class MediaController {
  constructor(private readonly mediaService: MediaService) {}
  @Post('upload')
  async upload(@Body() body: FormDataFile) {
    return this.mediaService.upload(body.file, body.dir.value);
  }

  @Get('loadImage/:id')
  @Public()
  async loadImage(
    @Param('id') id: string,
    @Res({ passthrough: true }) res: FastifyReply,
  ) {
    return await this.mediaService.loadImage(id, res);
  }
}
